/*
 * Project Name spring-boot-tools
 * File Name CacheStringHolder
 * Package Name com.huxiaosu.tools.common.holder
 * Create Time 2024/4/20
 * Create by name：liujie
 */
package com.huxiaosu.tools.common.holder;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.util.StringUtils;

/**
 * Description
 *
 * @author liujie
 * @date 2024/4/20 14:32
 */
@Slf4j
public class CacheStringHolder {

    private static final String TOOLS_CACHE_STRING = "__TOOLS_CACHE_STRING__";

    public static void set(String key, String value) {

        MDC.put(TOOLS_CACHE_STRING + key + "__", value);
    }

    public static String get(String key) {
        try {
            String value = MDC.get(TOOLS_CACHE_STRING + key + "__");
            if (StringUtils.hasText(value)) {
                return value;
            }
        }
        catch (Exception e) {
            log.error("get cache error ", e);
        }
        return null;
    }

    public static void remove(String key) {
        MDC.remove(TOOLS_CACHE_STRING + key + "__");
    }

    public static void clear() {
        MDC.clear();
    }

}
